from datetime import timedelta
import os
from flask import Flask, jsonify, render_template, request,session
from handle.S2Redis import socketio, sync_data_background
from api import api_bp
from handle.logger import logger
from flask import Flask
from config import Config
from object import init_app
from flask_cors import CORS



app = Flask(__name__)
CORS(app,supports_credentials=True)
app.config['JSON_SORT_KEYS'] = False
app.config['SECRET_KEY'] = '#z-f*C7xe>2G5cuJI&I2'
app.config['SESSION_TYPE'] = 'filesystem'
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=7)
# 设置上传文件存储路径
os.makedirs(Config.UPLOAD_FOLDER, exist_ok=True)
app.config['UPLOAD_FOLDER'] = Config.UPLOAD_FOLDER
# 初始化 SocketIO
socketio.init_app(app, cors_allowed_origins="*")  # 确保允许跨域
app.config.from_object(Config)
init_app(app)

# 注册 /api 蓝图
app.register_blueprint(api_bp, url_prefix='/api')

@socketio.on('connect')
def handle_connect():
    logger.info("Client connected:"+request.sid)
    print('Client connected:'+request.sid)

@socketio.on('disconnect')
def handle_disconnect():
    logger.info("Client disconnected:"+request.sid)
    print('Client disconnected:'+request.sid)



if __name__ == '__main__':
    sync_data_background()
    open('structured_logs.json', 'w').close()
    open('unstructured_logs.log', 'w').close()
    logger.info("Starting the server...")
    socketio.run(app, debug=True, log_output=True,host="0.0.0.0")
